<template>
  <div class="vux-color-picker">
    <el-row>
      <el-col :span="24/colors.length" v-for="color in colors" :key="color" class="vux-color-box">
        <span class="vux-color-item transition"
              :style="{borderRadius: width/2 + 'px',backgroundColor: color, width: width + 'px', height: width + 'px'}"
              @click="change(color)"
              :class="{'vux-color-white': color === '#fff' || color === '#fff', 'vux-color-picker-small': size === 'small', 'vux-color-picker-middle': size === 'middle'}">
          <i v-if="color === value"
             class="vux-color-checked el-icon-check"
             :style="{lineHeight: width + 'px'}"></i>
        </span>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  const sizeMap = {
    'large': 40,
    'middle': 30,
    'small': 20
  }
  export default {
    created () {
      this.currentValue = this.value
    },
    props: {
      colors: {
        type: Array,
        required: true
      },
      size: {
        type: String,
        default: 'large'
      },
      value: String
    },
    computed: {
      width () {
        return sizeMap[this.size]
      }
    },
    data () {
      return {
        currentValue: ''
      }
    },
    watch: {
      value (val) {
        this.currentValue = val
      },
      currentValue (color) {
        this.$emit('on-change', color)
        this.$emit('input', color)
      }
    },
    methods: {
      change (color) {
        this.currentValue = color
      }
    }
  }
</script>

<style lang="scss">
  .vux-color-box {
    text-align: center;
  }
  .vux-color-picker {
    font-size: 0;
  }
  .vux-color-item {
    display: inline-block;
    text-align: center;
    box-sizing: border-box;
    position: relative;
    cursor:pointer;
    &:hover{
      opacity:0.6;
     }
  }
  .vux-color-checked {
    width: 100%;
    color:#fff;
  }
  .vux-color-picker-small .vux-color-checked:before{
    font-size: 10px;
  }
  .vux-color-picker-middle .vux-color-checked:before{
    font-size: 18px;
  }
</style>
